import swap from './utils'

function bubbleSort (arr) {
  if (arr == null || arr.length < 2) {
    return
  }
  let swapped = false
  for (let i = 0; i < arr.length - 1; i++) {
    swapped = false
    for (let j = 0; j < arr.length - 1 - i; j++) {
      if (arr[j] > arr[j + 1]) {
        swap(arr, j, j + 1)
        swapped = true
      }
    }
    if (!swapped) {
      break
    }
  }
}

export default bubbleSort
